home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / dependantscan / source / argumentsdps.c next >
C/C++ Source or Header  |  1995-11-14  |  19KB  |  343 lines

  1. #define DEF_ARGUMENTSDPS_C
  2.  
  3. #include <exec/types.h>
  4. #include <workbench/startup.h>
  5. #include <proto/icon.h>
  6. #include <clib/dos_protos.h>
  7. #include <clib/exec_protos.h>
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. #include "DependantScan.h"
  14. #include "ProcessDirectory.h"
  15. #include "RequesterError.h"
  16.  
  17. /*
  18.     void parse_scoptions_for_argument(
  19.         char *scoptions_variable,                                            the string to look for in the scoptions file
  20.         int argument_number,                                                    the argument to assign it to
  21.         char *line_buffer)                                                    static buffer (of size PD_PATHMAX) to store lines and variable value
  22.  
  23.    * Description
  24.        This function will attempt to build the name of the scoptions file and get the specified SAS/C variable's value from that file. If
  25.        successful, the DPSArgument[argument_number] variable will be set to the value fetched from the scoptions file.
  26. */
  27. void parse_scoptions_for_argument(
  28.     char *scoptions_variable,                                                /* the string to look for in the scoptions file */
  29.     int argument_number,                                                        /* the argument to assign it to */
  30.     char *line_buffer)                                                        /* static buffer (of size PD_PATHMAX) to store lines and variable value */
  31. {
  32.     FILE *stream;                                                                /* will be connected to the file specified by 'scoptions_name' */
  33.     char scoptions_name[PD_PATHMAX];                            /* name of scoptions file */
  34.    size_t variable_length = strlen(scoptions_variable);            /* remember how long the scoptions variable is */
  35.  
  36.    if (DPSArgument[DPS_ARG_PATH] == NULL)                      /* if the current directory is to be used */
  37.        strcpy(scoptions_name,DPS_SCOPTIONS_FILENAME);           /* then, no path information for the scoptions file should be used */
  38.    else                                                                            /* either not the shell or the WB */
  39.        dps_build_path(scoptions_name,DPS_SCOPTIONS_FILENAME);    /* build the name of the scoptions file */
  40.  
  41.     if ((stream = fopen(scoptions_name,"r")) != NULL)                /* if we can open the file */
  42.     {
  43.         while (fgets(line_buffer,PD_PATHMAX,stream))             /* while we can get a line from the file */
  44.         {
  45.             if (!memcmp(line_buffer,scoptions_variable,variable_length))                           /* if this line mentions the variable we are looking for */
  46.             {
  47.                 DPSArgument[argument_number] = line_buffer + variable_length;                       /* get the variable's value */
  48.                 if (strchr(line_buffer,'\n'))                      /* if the line contains a new-line */
  49.                     *strchr(line_buffer,'\n') = '\0';               /* remove the new-line */
  50.  
  51.                 break;                                                            /* we are done */
  52.             }
  53.         }
  54.  
  55.         fclose(stream);                                                        /* clean up after ourselves */
  56.     }
  57. }
  58. /*
  59.     void parse_scoptions_for_project(void)
  60.  
  61.     * Description
  62.        This function will attempt to parse the value of DPSArgument[DPS_ARG_PROJECT] from the scoptions file. If successful, the
  63.        DPSArgument[DPS_ARG_PROJECT] variable will be set to the value fetched from the scoptions file.
  64. */
  65. void parse_scoptions_for_project(void)
  66. {
  67.    static char line_buffer[PD_PATHMAX];                                /* required by parse_scoptions_for_argument() */
  68.    parse_scoptions_for_argument(DPS_PROGRAMNAME_STRING,DPS_ARG_PROJECT,line_buffer);                /* actually do the parsing */
  69. }
  70.  
  71. /*
  72.     void parse_scoptions_for_object_dir(void)
  73.  
  74.     * Description
  75.        This function will attempt to parse the value of DPSArgument[DPS_ARG_OBJECT_DIR] from the scoptions file. If successful, the
  76.        DPSArgument[DPS_ARG_OBJECT_DIR] variable will be set to the value fetched from the scoptions file.
  77. */
  78. void parse_scoptions_for_object_dir(void)
  79. {
  80.    static char line_buffer[PD_PATHMAX];                                /* required by parse_scoptions_for_argument() */
  81.    parse_scoptions_for_argument(DPS_OBJECTNAME_STRING,DPS_ARG_OBJECT_DIR,line_buffer);                /* actually do the parsing */
  82. }
  83.  
  84. /*
  85.     BOOL dps_get_shell_arguments(
  86.        struct RDArgs *rdargs)                                   where to place the on-line help
  87.  
  88.    * Description
  89.        This function gets the command-line arguments when the program is spawned from the shell.
  90.  
  91.    * Return Value
  92.        TRUE = successful, a call to FreeArgs() is necessary
  93.       FALSE = unsuccessful, a call to FreeArgs() is not necessary
  94. */
  95. BOOL dps_get_shell_arguments(
  96.    struct RDArgs *rdargs)                                      /* where to place the on-line help */
  97. {
  98.     BOOL free_args_needed = FALSE;                              /* value that is returned, TRUE is good, FALSE is bad */
  99.     char *project;                                              /* tells if the user specified a project name */
  100.     char *object_dir;                                                            /* for determining if the user specified an object directory */
  101.    static long files_on_line = DPS_DEFAULT_FILESONLINE;        /* the default number of files per line */
  102.    int index;                                                                    /* for accessing our messages */
  103.    char arg_lower_name[40];                                    /* lower case version of a command line option */
  104.    char *arg_type;                                                            /* the type of the current argument */
  105.    char *arg_name;                                                            /* for pointing to various parts of the the current argument */
  106.    char *next_text;                                            /* where the next bit of text should go */
  107.    int longest_arg_name = 0;                                                /* the length of the longest argument name */
  108.    char *template = NULL;                                                    /* the template as supplied to DOS */
  109.  
  110.    if ((rdargs->RDA_ExtHelp = AllocVec(2048,0)) == NULL)       /* if we cannot get some memory for our help text */
  111.        {quick_requester_error(DPS_ERROR_CANNOT_ALLOCATE,DPS_ERROR_CANNOT_ALLOCATE,2048); goto _ABORT;}
  112.  
  113.    if ((template = AllocVec(DPS_LINE_BUFFER_SIZE,0)) == NULL)    /* if we cannot get some memory for the DOS template */
  114.        {quick_requester_error(DPS_ERROR_CANNOT_ALLOCATE,DPS_ERROR_CANNOT_ALLOCATE,DPS_LINE_BUFFER_SIZE); goto _ABORT;}
  115.  
  116.     strcpy(rdargs->RDA_ExtHelp,dps_locale_string(DPS_MSG_USAGE));                                            /* intialize the help text */
  117.     next_text = rdargs->RDA_ExtHelp + strlen(rdargs->RDA_ExtHelp);                               /* setup where to start appending information */
  118.  
  119.     for (index = 0; index < DPS_ARG_COUNT; ++index)                    /* access all of the command line options */
  120.     {
  121.         arg_type = dps_locale_string(DPS_ARG_COUNT+1+index);     /* get the type of argument we are dealing with */
  122.         arg_name = dps_locale_string(index);                     /* get the name of the argument */
  123.  
  124.         if (strlen(arg_name) > longest_arg_name)                        /* if this argument is even longer than the others */
  125.             longest_arg_name = strlen(arg_name);                        /* remember which one is the longest */
  126.  
  127.         if (strstr(arg_type,"/K"))                               /* if this is a key word */
  128.         {
  129.          strcpy(arg_lower_name,arg_name);                                /* get a working copy of the argument name */
  130.          strlwr(arg_lower_name);                                            /* convert the copy to lower case */
  131.             next_text += sprintf(next_text," [%s %s]",arg_name,arg_lower_name);
  132.       }
  133.       else if (strstr(arg_type,"/S"))                                    /* if this is a switch type argument */
  134.       {
  135.          next_text += sprintf(next_text," [%s]",arg_name);
  136.       }
  137.       else                                                                        /* NOTE: handle other cases as needed */
  138.       {
  139.          next_text += sprintf(next_text,"%s",arg_name);
  140.       }
  141.  
  142.       if (index == (DPS_ARG_COUNT/2))                                    /* if we are at the half way mark */
  143. /* put a break in the arguments */
  144.           next_text += sprintf(next_text,"\n%*s",strlen(dps_locale_string(DPS_MSG_USAGE)),"");
  145.     }
  146.     
  147.    next_text += sprintf(next_text,"\n");                                /* put the description of each argument starting on the next line */
  148.  
  149.     for (index = 0; index < DPS_ARG_COUNT; ++index)                    /* access all of the command line options again */
  150.     {
  151.       strcpy(arg_lower_name,dps_locale_string(index));            /* get a working copy of the argument name */
  152.       strlwr(arg_lower_name);                                  /* convert the copy to lower case */
  153. /* display the help for this command line option on it's own line */
  154.         next_text += sprintf(next_text,"\t%*s %s\n",longest_arg_name,arg_lower_name,dps_locale_string(DPS_MSG_USAGE+1+index));
  155.    }
  156.  
  157.    next_text = template;                                       /* let's start building the DOS template */
  158.  
  159.     for (index = 0; index < DPS_ARG_COUNT; ++index)                    /* access all of the command line options One More Time */
  160.         next_text += sprintf(next_text,dps_locale_string(DPS_ARG_COUNT+1+index),dps_locale_string(index));
  161.  
  162.    DPSArgument[DPS_ARG_PROJECT] = DPS_DEFAULT_PROJECT;         /* setup shell default arguments */
  163.    DPSArgument[DPS_ARG_MATCH] = DPS_DEFAULT_MATCH;
  164.     DPSArgument[DPS_ARG_MAKEFILE] = DPS_DEFAULT_MAKEFILE;
  165.    DPSArgument[DPS_ARG_OBJECT_DIR] = DPS_DEFAULT_OBJECT_DIR;
  166.    DPSArgument[DPS_ARG_FILESONLINE] = (char *)&files_on_line;
  167.     DPSArgument[DPS_ARG_FROM] = DPS_DEFAULT_FROM;
  168.     DPSArgument[DPS_ARG_LIBRARY] = DPS_DEFAULT_LIBRARY;
  169.  
  170.    project = DPSArgument[DPS_ARG_PROJECT];                     /* remember the default project name */
  171.    object_dir = DPSArgument[DPS_ARG_OBJECT_DIR];                    /* remember the default object directory */
  172.  
  173.    if (ReadArgs(template,(LONG *)DPSArgument,rdargs) == 0)     /* if we cannot read the arguments */
  174.       goto _ABORT;
  175.  
  176.    free_args_needed = TRUE;                                    /* note that we did it */
  177.  
  178.     if (project == DPSArgument[DPS_ARG_PROJECT])                /* if the user did not specify a project */
  179.         parse_scoptions_for_project();                           /* then, maybe the answer is in the scoptions file */
  180.  
  181.    if (object_dir == DPSArgument[DPS_ARG_OBJECT_DIR])                /* if the user did not specify an object directory */
  182.        parse_scoptions_for_object_dir();                                /* then, maybe the value is in the scoptions file */
  183.  
  184. _ABORT:
  185.     if (rdargs->RDA_ExtHelp)                                                /* if we allocated some help text */
  186.       FreeVec(rdargs->RDA_ExtHelp);                                        /* clean up after ourselves */
  187.  
  188.     if (template)                                                                /* if we allocated this */
  189.         FreeVec(template);                                                    /* clean up after ourselves */
  190.  
  191.    return (free_args_needed);
  192. }
  193.  
  194. /*
  195.     int dps_process_workbench_arguments(
  196.         struct WBStartup *wb_startup)                                        the startup message that WorkBench sends us (argv under SAS/C)
  197.  
  198.    * Description
  199.        This function parses the arguments from the tool types of each selected project (or the program's icon if need be) and generates a
  200.        makefile for each.
  201.  
  202.    * Return Value
  203.        0 = good
  204.        other = error code from dependant_scan()
  205. */
  206. int dps_process_workbench_arguments(
  207.     struct WBStartup *wb_startup)                                            /* the startup message that WorkBench sends us (argv under SAS/C) */
  208. {
  209.     BPTR startup_directory;                                                    /* the main directory we should switch back to */
  210.     struct DiskObject *icon = NULL;                                        /* the icon we are examining */
  211.     struct WBArg *wb_arg;                                                    /* current argument from the wb_startup->sm_ArgList array */
  212.     struct WBArg *last_wb_arg = wb_startup->sm_ArgList+wb_startup->sm_NumArgs;                   /* points to one past the last workbench argument */
  213.    char *rvalue;                                                                /* the value to the right of the equals sign on tool types */
  214.    static long files_on_line = DPS_DEFAULT_FILESONLINE;        /* the default number of files per line */
  215.     int retval;                                                 /* value from dependant_scan() */
  216.  
  217.     startup_directory = CurrentDir(wb_startup->sm_ArgList->wa_Lock);                                        /* note the absolute directory to switch back to */
  218.  
  219.     if (wb_startup->sm_NumArgs > 1)                                        /* if it is possible that a project icon was double clicked */
  220.     {                                                           /* we are going to scan for project icons that we can process */
  221.        for (wb_arg = wb_startup->sm_ArgList+1; wb_arg < last_wb_arg; ++wb_arg)                   /* access the icons that are not our program's icon */
  222.        {
  223.            if (wb_arg->wa_Name && (*wb_arg->wa_Name) && wb_arg->wa_Lock)                          /* if this icon is not a disk, directory or trashcan and the icon type supports locks */
  224.                break;                                                            /* then, there is at least one icon we can process */
  225.        }
  226.  
  227.       if (wb_arg < last_wb_arg)                                            /* if we found a valid project icon */
  228.             wb_arg = wb_startup->sm_ArgList+1;                    /* start at the first project icon */
  229.         else                                                     /* no valid project icons were selected */
  230.             wb_arg = wb_startup->sm_ArgList;                      /* use our tool as if it were a project */
  231.     }
  232.     else                                                                            /* only the tool icon was passed to us */
  233.     {
  234.         wb_arg = wb_startup->sm_ArgList;                         /* use the tool as if it were a project */
  235.     }
  236.  
  237.     for (; wb_arg < last_wb_arg; ++wb_arg)                      /* access our icons */
  238.    {
  239.        if ((wb_arg->wa_Name == NULL) || (*wb_arg->wa_Name == '\0') || (wb_arg->wa_Lock == NULL)) /* if this icon is a disk, directory or trashcan or the icon type does not supports locks */
  240.            continue;                                                            /* then, ignore the icon */
  241.  
  242.       if (icon)                                                                /* if we have a previous icon taking up memory */
  243.        {
  244.           FreeDiskObject(icon);                                 /* don't need the icon taking up memory anymore */
  245.           icon = NULL;                                                        /* note that it has been free'd */
  246.       }
  247.  
  248.         CurrentDir(wb_arg->wa_Lock);                             /* switch to the icon's directory */
  249.  
  250.         if ((icon = GetDiskObject(wb_arg->wa_Name)) == NULL)     /* if we cannot get information on this icon */
  251.             continue;                                                            /* then, ignore the icon */
  252.  
  253.         rvalue = FindToolType(icon->do_ToolTypes,dps_locale_string(DPS_ARG_PATH));                /* attempt to locate an alternate path to be scanned */
  254.       if (rvalue && (*rvalue == '\0'))                         /* if zero length string was specified */
  255.           DPSArgument[DPS_ARG_PATH] = NULL;                            /* use the current directory */
  256.       else
  257.          DPSArgument[DPS_ARG_PATH] = rvalue;                            /* otherwise use specified or non-existant path */
  258.  
  259.         rvalue = FindToolType(icon->do_ToolTypes,dps_locale_string(DPS_ARG_PROJECT));             /* attempt to get the name of our project from the icon */
  260.       if ((rvalue == NULL) || (rvalue[0] == '\0'))             /* if no project was supplied */
  261.       {
  262.           DPSArgument[DPS_ARG_PROJECT] = NULL;                  /* start with no project name */
  263.  
  264.             parse_scoptions_for_project();                        /* maybe the answer is in the scoptions file */
  265.             if (DPSArgument[DPS_ARG_PROJECT] == NULL)                    /* if we still don't have a project name */
  266.                DPSArgument[DPS_ARG_PROJECT] = DPS_DEFAULT_PROJECT;                                 /* then, use the default name */
  267.       }
  268.       else                                                                        /* a project name was supplied */
  269.       {
  270.           DPSArgument[DPS_ARG_PROJECT] = rvalue;                        /* use the project name from the icon */
  271.       }
  272.  
  273.       rvalue = FindToolType(icon->do_ToolTypes,dps_locale_string(DPS_ARG_MATCH));               /* search for the file pattern specifier */
  274.       if (rvalue && *rvalue)                                   /* if a valid file pattern was specified */
  275.           DPSArgument[DPS_ARG_MATCH] = rvalue;                        /* use the specified file pattern */
  276.       else                                                                        /* nothing specified */
  277.            DPSArgument[DPS_ARG_MATCH] = DPS_DEFAULT_MATCH;            /* use the default value */
  278.  
  279.       rvalue = FindToolType(icon->do_ToolTypes,dps_locale_string(DPS_ARG_MAKEFILE));            /* search for the name of the makefile */
  280.       if (rvalue && *rvalue)                                   /* if a valid value was specified */
  281.           DPSArgument[DPS_ARG_MAKEFILE] = rvalue;               /* use the specified value */
  282.       else                                                                        /* nothing specified */
  283.            DPSArgument[DPS_ARG_MAKEFILE] = DPS_DEFAULT_MAKEFILE; /* use the default value */
  284.  
  285.       rvalue = FindToolType(icon->do_ToolTypes,dps_locale_string(DPS_ARG_RULES));               /* search for the name of a file to be inserted into the makefile */
  286.       if (rvalue && *rvalue)                                   /* if a valid value was specified */
  287.           DPSArgument[DPS_ARG_RULES] = rvalue;                  /* use the specified value */
  288.       else                                                                        /* nothing specified */
  289.            DPSArgument[DPS_ARG_RULES] = NULL;                    /* use the default value */
  290.  
  291.       rvalue = FindToolType(icon->do_ToolTypes,dps_locale_string(DPS_ARG_OBJECT_DIR));          /* search for the name of the object file directory */
  292.  
  293.       if ((rvalue == NULL) || (rvalue[0] == '\0'))                    /* if no object directory was specified */
  294.       {
  295.           DPSArgument[DPS_ARG_OBJECT_DIR] = NULL;                    /* start with no object directory */
  296.  
  297.          parse_scoptions_for_object_dir();                            /* try to get the object directory from the scoptions file */
  298.          if (DPSArgument[DPS_ARG_OBJECT_DIR] == NULL)                /* if we still don't have an object directory name */
  299.              DPSArgument[DPS_ARG_OBJECT_DIR] = DPS_DEFAULT_OBJECT_DIR;                                    /* use the default object directory name */
  300.       }
  301.       else                                                                        /* an object directory was specified */
  302.       {
  303.           DPSArgument[DPS_ARG_OBJECT_DIR] = rvalue;             /* use the specified value */
  304.       }
  305.  
  306.       rvalue = FindToolType(icon->do_ToolTypes,dps_locale_string(DPS_ARG_FILESONLINE));         /* search for the maximum number of files per line in the makefile */
  307.       if (rvalue && *rvalue && atoi(rvalue))                   /* if a valid value was specified */
  308.           files_on_line = atoi(rvalue);                         /* use the specified value */
  309.       else                                                                        /* nothing specified */
  310.            files_on_line = DPS_DEFAULT_FILESONLINE;              /* use the default number of files per line */
  311.  
  312.        DPSArgument[DPS_ARG_FILESONLINE] = (char *)&files_on_line;                                            /* always use our number of filesonline */
  313.  
  314.       rvalue = FindToolType(icon->do_ToolTypes,dps_locale_string(DPS_ARG_FROM));                /* search for the bit of text that follows the linker FROM keyword */
  315.       if (rvalue && *rvalue)                                   /* if a valid value was specified */
  316.           DPSArgument[DPS_ARG_FROM] = rvalue;                   /* use the specified value */
  317.       else                                                                        /* nothing specified */
  318.            DPSArgument[DPS_ARG_FROM] = DPS_DEFAULT_FROM;         /* use the default value */
  319.  
  320.       rvalue = FindToolType(icon->do_ToolTypes,dps_locale_string(DPS_ARG_LIBRARY));             /* search for the libraries to be included */
  321.       if (rvalue && *rvalue)                                   /* if a valid value was specified */
  322.           DPSArgument[DPS_ARG_LIBRARY] = rvalue;                        /* use the specified value */
  323.       else                                                                        /* nothing specified */
  324.            DPSArgument[DPS_ARG_LIBRARY] = DPS_DEFAULT_LIBRARY;   /* use the default value */
  325.  
  326.       rvalue = FindToolType(icon->do_ToolTypes,dps_locale_string(DPS_ARG_VERBOSE));             /* search for the verbose switch */
  327.       if (rvalue)                                              /* if the keyword exists */
  328.           DPSArgument[DPS_ARG_VERBOSE] = (char *)(TRUE);        /* note that it exists */
  329.       else                                                                        /* nothing specified */
  330.            DPSArgument[DPS_ARG_VERBOSE] = (char *)(FALSE);       /* use the default value */
  331.  
  332.       if ((retval = dependant_scan()) != 0)                            /* if we have trouble doing this scan */
  333.           break;                                                                /* then, we are outta here */
  334.    }
  335.  
  336. _ABORT:
  337.     if (icon)                                                   /* if we have an icon taking up memory */
  338.         FreeDiskObject(icon);                                                /* don't need the icon taking up memory */
  339.  
  340.     CurrentDir(startup_directory);                              /* switch back to the startup directory */
  341.     return (retval);                                                            /* here ya go */
  342. }
  343.